package com.netease.xone.activity;

import Utils.StackTraceUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.dataMgr.ParcelableArgument;
import com.netease.xone.fragment.FragmentDiscuss;

public class ActivityDiscuss extends ActivityX1Base {
	private static final String TAG = ActivityDiscuss.class.getSimpleName();
	private FragmentDiscuss mFragment = null;

	public static void launch(Context context, Parcelable value){
		Intent intent = new Intent(context, ActivityDiscuss.class);
		//非activity的context启动activity会出错
		if(!(context instanceof Activity))
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(XoneConsts.BUNDLE_KEY.DISCUSS_TYPE, value);
		context.startActivity(intent);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    NTLog.d(TAG, StackTraceUtil.getMethodName()
                + " savedInstanceState=" + (null == savedInstanceState ? "null" : savedInstanceState.toString()));
		super.onCreate(savedInstanceState);
		
		FrameLayout linear = new FrameLayout(this);
        linear.setId(R.id.activity_discuss_container_id);
        setContentView(linear);
        
		if (null != findViewById(R.id.activity_discuss_container_id) && savedInstanceState == null) {
			if (null == savedInstanceState) {
				FragmentManager fm = getSupportFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();

				ParcelableArgument arg = getIntent().getParcelableExtra(XoneConsts.BUNDLE_KEY.DISCUSS_TYPE);
				mFragment = FragmentDiscuss.newInstance(arg);
				ft.replace(R.id.activity_discuss_container_id, mFragment, TAG);
				ft.commit();
			}
		}
	}
	
	@Override
	protected void onResume() {
	    getWindow().setWindowAnimations(R.style.window_anim_left_in_right_out);
	    super.onResume();
	}
	
	@Override
	public void onBackPressed() {
	    boolean bRes = false;
	    if (null != mFragment) {
	        bRes = mFragment.onBackPressed();
	        
	        if (!bRes) {
	            super.onBackPressed();
	        }
	    }
	}
	
	@Override
	protected void onDestroy() {
	    NTLog.d(TAG, StackTraceUtil.getMethodName());
		super.onDestroy();
		mFragment = null;
	}
}
